package com.rex.saas.controller;

import cn.hutool.core.date.DateUtil;
import com.rex.saas.bean.BaseResponse;
import com.rex.saas.bean.ResultCode;
import com.rex.saas.bean.request.SpaceEnergyRequest;
import com.rex.saas.bean.request.SpaceInComeRequest;
import com.rex.saas.bean.request.SpacePowerRequest;
import com.rex.saas.constants.Constants;
import com.rex.saas.db.entity.SaasSpaceInfo;
import com.rex.saas.service.SpaceService;
import com.rex.saas.service.StatisticsService;
import com.rex.saas.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @ClassName StatisticsController
 * @Description:
 * @Author: zhusiyu
 * @CreateDate: 2025/03/18 10:55	//创建时间
 */
@RestController
@Slf4j
@RequestMapping("/statistics")
public class StatisticsController extends BaseController{
    @Autowired
    private SpaceService spaceService;

    @Autowired
    private StatisticsService statisticsService;

    @PostMapping("/space/power")
    public BaseResponse spacePower(HttpServletRequest request, @RequestBody SpacePowerRequest spacePowerRequest){
        log.info("spacePower ===> spacePowerRequest:{}", spacePowerRequest);
        if (spacePowerRequest==null || spacePowerRequest.getSpaceId()==null || spacePowerRequest.getRecordDate()==null){
            log.error("spacePower is null, spacePowerRequest:{}", spacePowerRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        SaasSpaceInfo space = spaceService.getSpaceById(spacePowerRequest.getSpaceId());
        if(space==null){
            log.error("spacePower space is null,spaceInfoId:{}", spacePowerRequest.getSpaceId());
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        try {
            if(spacePowerRequest.getRecordType() == Constants.STATISTICS_DAY){
                Date date = DateUtil.parse(spacePowerRequest.getRecordDate(), "yyyy-MM-dd");
            }else if(spacePowerRequest.getRecordType() == Constants.STATISTICS_MONTH){
                Date date = DateUtil.parse(spacePowerRequest.getRecordDate(), "yyyy-MM");
            }else if(spacePowerRequest.getRecordType() == Constants.STATISTICS_YEAR){
                Date date = DateUtil.parse(spacePowerRequest.getRecordDate(), "yyyy");
            }
        }catch (Exception e){
            log.error("spacePower recordDate is error, spaceEnergyRequest:{}", spacePowerRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        String token = request.getHeader("token");
        if(!JwtUtils.getTenantId(token).equals(space.getTenantId())){
            log.error("spacePower space tenantId not match, tenantId:{}, saasSpaceInfo:{}", JwtUtils.getTenantId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN) && !JwtUtils.getCustomerId(token).equals(space.getCustomerId())){
            log.error("spacePower space customerId not match, customerId:{}, saasSpaceInfo:{}", JwtUtils.getCustomerId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        String tenantToken = getCustomerSpaceTenantToken(token, spacePowerRequest.getSpaceId());
        if(spacePowerRequest.getRecordType()==1){
            return BaseResponse.ok(statisticsService.getDayPower(spacePowerRequest.getSpaceId(), spacePowerRequest.getRecordDate(), tenantToken));
        }else{
            log.error("spacePower type is error, spacePowerRequest:{}", spacePowerRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
    }

    @PostMapping("/space/energy")
    public BaseResponse spaceEnergy(HttpServletRequest request, @RequestBody SpaceEnergyRequest spaceEnergyRequest){
        log.info("spaceEnergy ===> spaceEnergyRequest:{}", spaceEnergyRequest);
        if (spaceEnergyRequest==null || spaceEnergyRequest.getSpaceId()==null || spaceEnergyRequest.getRecordDate()==null){
            log.error("spaceEnergy is null, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        SaasSpaceInfo space = spaceService.getSpaceById(spaceEnergyRequest.getSpaceId());
        if(space==null){
            log.error("spaceEnergy space is null,spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        try {
            if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_DAY){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM-dd");
            }else if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_MONTH){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM");
            }else if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_YEAR){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy");
            }
        }catch (Exception e){
            log.error("spaceEnergy recordDate is error, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        String token = request.getHeader("token");
        if(!JwtUtils.getTenantId(token).equals(space.getTenantId())){
            log.error("spaceEnergy space tenantId not match, tenantId:{}, saasSpaceInfo:{}", JwtUtils.getTenantId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN) && !JwtUtils.getCustomerId(token).equals(space.getCustomerId())){
            log.error("spaceEnergy space customerId not match, customerId:{}, saasSpaceInfo:{}", JwtUtils.getCustomerId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        if(spaceEnergyRequest.getRecordType()==1){
            return BaseResponse.ok(statisticsService.getDayEnergy(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==2){
            return BaseResponse.ok(statisticsService.getMonthEnergy(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==3){
            return BaseResponse.ok(statisticsService.getYearEnergy(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==4){
            return BaseResponse.ok(statisticsService.getAllEnergy(spaceEnergyRequest.getSpaceId()));
        }else{
            log.error("spaceEnergy type is error, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
    }

    @PostMapping("/space/energy/v3")
    public BaseResponse spaceEnergyV3(HttpServletRequest request, @RequestBody SpaceEnergyRequest spaceEnergyRequest){
        log.info("spaceEnergyV3 ===> spaceEnergyRequest:{}", spaceEnergyRequest);
        if (spaceEnergyRequest==null || spaceEnergyRequest.getSpaceId()==null
                || spaceEnergyRequest.getRecordDate()==null){
            log.error("spaceEnergyV3 is null, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        SaasSpaceInfo space = spaceService.getSpaceById(spaceEnergyRequest.getSpaceId());
        if(space==null){
            log.error("spaceEnergyV3 space is null,spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        try {
            if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_DAY){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM-dd");
            }else if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_MONTH){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM");
            }else if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_YEAR){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy");
            }
        }catch (Exception e){
            log.error("spaceEnergyV3 recordDate is error, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        String token = request.getHeader("token");
        if(!JwtUtils.getTenantId(token).equals(space.getTenantId())){
            log.error("spaceEnergyV3 space tenantId not match, tenantId:{}, saasSpaceInfo:{}", JwtUtils.getTenantId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN) && !JwtUtils.getCustomerId(token).equals(space.getCustomerId())){
            log.error("spaceEnergyV3 space customerId not match, customerId:{}, saasSpaceInfo:{}", JwtUtils.getCustomerId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        if(spaceEnergyRequest.getRecordType()==1){
            return BaseResponse.ok(statisticsService.getDayPowerDetail(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==2){
            return BaseResponse.ok(statisticsService.getMonthEnergyDetail(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==3){
            return BaseResponse.ok(statisticsService.getYearEnergyDetail(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==4){
            return BaseResponse.ok(statisticsService.getAllEnergyDetail(spaceEnergyRequest.getSpaceId()));
        }else{
            log.error("spaceEnergyV2 type is error, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
    }


    @PostMapping("/space/energy/v2")
    public BaseResponse spaceEnergyV2(HttpServletRequest request, @RequestBody SpaceEnergyRequest spaceEnergyRequest){
        log.info("spaceEnergyV2 ===> spaceEnergyRequest:{}", spaceEnergyRequest);
        if (spaceEnergyRequest==null || spaceEnergyRequest.getSpaceId()==null
                || spaceEnergyRequest.getRecordDate()==null){
            log.error("spaceEnergyV2 is null, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        SaasSpaceInfo space = spaceService.getSpaceById(spaceEnergyRequest.getSpaceId());
        if(space==null){
            log.error("spaceEnergyV2 space is null,spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        try {
            if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_DAY){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM-dd");
            }else if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_MONTH){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM");
            }else if(spaceEnergyRequest.getRecordType() == Constants.STATISTICS_YEAR){
                Date date = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy");
            }
        }catch (Exception e){
            log.error("spaceEnergyV2 recordDate is error, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        String token = request.getHeader("token");
        if(!JwtUtils.getTenantId(token).equals(space.getTenantId())){
            log.error("spaceEnergyV2 space tenantId not match, tenantId:{}, saasSpaceInfo:{}", JwtUtils.getTenantId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN) && !JwtUtils.getCustomerId(token).equals(space.getCustomerId())){
            log.error("spaceEnergyV2 space customerId not match, customerId:{}, saasSpaceInfo:{}", JwtUtils.getCustomerId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        if(spaceEnergyRequest.getRecordType()==1){
            return BaseResponse.ok(statisticsService.getDayEnergyDetail(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==2){
            return BaseResponse.ok(statisticsService.getMonthEnergyDetail(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==3){
            return BaseResponse.ok(statisticsService.getYearEnergyDetail(spaceEnergyRequest));
        }else if(spaceEnergyRequest.getRecordType()==4){
            return BaseResponse.ok(statisticsService.getAllEnergyDetail(spaceEnergyRequest.getSpaceId()));
        }else{
            log.error("spaceEnergyV2 type is error, spaceEnergyRequest:{}", spaceEnergyRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
    }

    @PostMapping("/space/income")
    public BaseResponse spaceIncome(HttpServletRequest request, @RequestBody SpaceInComeRequest spaceInComeRequest){
        log.info("spaceIncome ===> spaceInComeRequest:{}", spaceInComeRequest);
        if (spaceInComeRequest==null || spaceInComeRequest.getSpaceId()==null || spaceInComeRequest.getYear()==null){
            log.error("spaceInComeRequest is null, spaceInComeRequest:{}", spaceInComeRequest);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        try {
            Date date = DateUtil.parse(spaceInComeRequest.getYear(), "yyyy");
        }catch (Exception e){
            log.error("spaceInComeRequest recordDate is error, spaceInComeRequest:{}", spaceInComeRequest, e);
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }
        SaasSpaceInfo space = spaceService.getSpaceById(spaceInComeRequest.getSpaceId());
        if(space==null){
            log.error("spaceInComeRequest space is null,spaceInfoId:{}", spaceInComeRequest.getSpaceId());
            return BaseResponse.error(ResultCode.PARAM_ERROR);
        }

        String token = request.getHeader("token");
        if(!JwtUtils.getTenantId(token).equals(space.getTenantId())){
            log.error("spaceInComeRequest space tenantId not match, tenantId:{}, saasSpaceInfo:{}", JwtUtils.getTenantId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN) && !JwtUtils.getCustomerId(token).equals(space.getCustomerId())){
            log.error("spaceInComeRequest space customerId not match, customerId:{}, saasSpaceInfo:{}", JwtUtils.getCustomerId(token), space);
            return BaseResponse.error(ResultCode.PERMISSION_ERROR);
        }

        return BaseResponse.ok(statisticsService.getYearInCome(spaceInComeRequest));
    }

}
